<?php

namespace support\sms\engine;

use exception\ApiException;
use support\utils\Random;
use think\Exception;
use support\utils\Cache;

/**
 * 短信服务类
 */
abstract class Server
{
    protected $phone;  //手机号

    protected $error;
    protected $smsCode;

    /**
     * 构造函数
     * Server constructor.
     * @throws Exception
     */
    protected function __construct()
    {
        //接收短信的手机号
        $this->phone = request()->post("phone");
        if (empty($this->phone)) {
            throw new ApiException("请先填写手机号");
        }
        // 生成发送短信的验证码
        $this->smsCode = $this->buildSendSms();
    }

    /**
     * 发送短信
     * @return mixed
     */
    abstract protected function sendSms();

    /**
     * 返回错误信息
     * @return mixed
     */
    public function getError()
    {
        return $this->error;
    }

     /**
     * 返回验证码
     * @return mixed
     */
    public function getCode()
    {
        return $this->smsCode;
    }

    /**
     * @Notes: 生成短信验证码
     * @Interface buildSaveName
     * @return string
     * @author: Mr.Zhang
     * @copyright: 河南八六互联信息技术有限公司
     * @Time: 2021/5/14   8:17 下午
     */
    private function buildSendSms()
    {
       return Random::numeric(6);
    }

    /**
     * @Notes: 发送短信
     * @Interface request_post
     * @param string $url
     * @param $post_data
     * @return bool|string
     * @author: Mr.Zhang
     * @copyright: 河南八六互联信息技术有限公司
     * @Time: 2021/3/11   9:52 上午
     */
    public function requestPost($url = '', $post_data,$is_json = false) {
        if (empty($url) || empty($post_data)) {
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($is_json)
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: ' . strlen($post_data)
                )
            );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $response;
    }

    /**
     * 格式化参数格式化成url参数
     * @param $values
     * @return string
     */
    public function toUrlParams($values)
    {
        $buff = '';
        foreach ($values as $k => $v) {
            if ($k != 'sign' && $v != '' && !is_array($v)) {
                $buff .= $k . '=' . $v . '&';
            }
        }
        return trim($buff, '&');
    }

    /**
     * @Notes: 防止防止恶意攻击
     * @Interface sms_safe
     * @author: Mr.Zhang
     * @copyright: 河南八六互联信息技术有限公司
     * @Time: 2021/5/14   11:36 下午
     */
    public function sms_safe($config)
    {
        if (!empty($_SESSION['sms_send_black']) && $_SESSION['sms_send_black'] + $config['sms_send_black_time'] > time()) {
            throw new ApiException( '操作频繁,请'.ceil(($_SESSION['sms_send_black'] + $config['sms_send_black_time'] - time())/60).'分钟后重试');
        }
        if (empty($_SESSION['sms_send_num'])) {
            $_SESSION['sms_send_num'] = 1;
        }
        if(!empty($_SESSION['sms_send_time']) && $_SESSION['sms_send_time'] + $config['sms_send_time'] > time()){
            throw new ApiException('操作频繁,请'.($_SESSION['sms_send_time'] + $config['sms_send_time'] - time()).'秒后重试');
        }
        if ($_SESSION['sms_send_num'] > $config['sms_send_num']) {
            $_SESSION['sms_send_black'] = time();
            unset($_SESSION['sms_send_num']);
            unset($_SESSION['sms_send_time']);
            throw new ApiException('发送次数超过限制');
        }
    }
}
